package com.xbongbong.paas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.dao.BusinessRuleInfoDao;
import com.xbongbong.paas.domain.entity.BusinessRuleInfoEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.BusinessRuleInfoModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.StringConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/5/9 19:48
 * @since v1.0
 */
@Service("businessRuleInfoModel")
public class BusinessRuleInfoModelImpl implements BusinessRuleInfoModel {

    @Resource
    private BusinessRuleInfoDao businessRuleInfoDao;

    @Override
    public Integer insert(BusinessRuleInfoEntity entity) {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return businessRuleInfoDao.insert(entity);
    }

    @Override
    public Integer update(BusinessRuleInfoEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setUpdateTime(now);
        return businessRuleInfoDao.update(entity);
    }

    @Override
    public List<BusinessRuleInfoEntity> findEntitys(Map<String, Object> param) {
        return businessRuleInfoDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return businessRuleInfoDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer delete(String corpid, Long id) {
        return businessRuleInfoDao.deleteByKey(id, corpid);
    }

    /**
     * 批量删除
     * @param list 主键id集合
     * @param corpid
     * @author xingxing.xiao
     */
    @Override
    public Integer deleteBatch(List<Long> list, String corpid) {
        return businessRuleInfoDao.deleteBatch(list, corpid);
    }

    /**
     * 批量更新
     * @param list
     * @param corpid
     * @author xingxing.xiao
     */
    @Override
    public void updateBatch(List<BusinessRuleInfoEntity> list, String corpid) {
        businessRuleInfoDao.updateBatch(list, corpid);
    }

    @Override
    public List<BusinessRuleInfoEntity> getByFormId(Long formId, String corpid, Integer businessType) {
        return businessRuleInfoDao.getByFormId(formId, businessType, corpid);
    }

    @Override
    public Integer countByFormId(Long formId, String corpid, Integer businessType) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpid);
        param.put(StringConstant.FORM_ID, formId);
        param.put(StringConstant.BUSINESS_TYPE, businessType);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        return getEntitysCount(param);
    }

    @Override
    public List<BusinessRuleInfoEntity> insertBatch(List<BusinessRuleInfoEntity> list) {
        businessRuleInfoDao.insertBatch(list);
        return list;
    }

    @Override
    public Integer updatePriority(List<BusinessRuleInfoEntity> businessRuleInfoList, String corpid) {
        return businessRuleInfoDao.updatePriority(businessRuleInfoList, corpid);
    }

    @Override
    public Integer updateEnable(Long id, Integer activeEnable, Integer inactiveEnable, String corpid) {
        return businessRuleInfoDao.updateEnable(id, activeEnable, inactiveEnable, corpid);
    }

    @Override
    public Integer deleteByForm(Long formId, Integer businessType, String corpid) {
        return businessRuleInfoDao.deleteByForm(formId, businessType, corpid);
    }
}
